<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item :label="t('student.fields.batchId')" prop="batchId">
        <el-select
          v-model="queryParams.batchId"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.batchId')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in batchOptions"
            :key="index"
            :label="item.batchName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.studentName')" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studentName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.recruitType')" prop="recruitType">
        <el-select
          v-model="queryParams.recruitType"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.recruitType')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('student.index.typeRecommended')" value="预推免" />
          <el-option :label="t('student.index.typeSummerCamp')" value="中国科大夏令营" />
          <el-option :label="t('student.index.typeUnifiedRetest')" value="统考复试" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.phone')" prop="phone">
        <el-input
          v-model="queryParams.phone"
          :placeholder="t('common.inputTextPlaceholder') + t('student.fields.phone')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.is985')" prop="is985">
        <el-select
          v-model="queryParams.is985"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.is985')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('common.yes')" :value="true" />
          <el-option :label="t('common.no')" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.is211')" prop="is211">
        <el-select
          v-model="queryParams.is211"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.is211')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('common.yes')" :value="true" />
          <el-option :label="t('common.no')" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.isFirstClass')" prop="isFirstClass">
        <el-select
          v-model="queryParams.isFirstClass"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.isFirstClass')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('common.yes')" :value="true" />
          <el-option :label="t('common.no')" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.recruitStatus')" prop="recruitStatus">
        <el-select
          v-model="queryParams.recruitStatus"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.recruitStatus')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('student.index.rsApplied')" value="已报名" />
          <el-option :label="t('student.index.rsNotApplied')" value="未报名" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewQuality')" prop="interviewQuality">
        <el-select
          v-model="queryParams.interviewQuality"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewQuality')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('student.index.iq_unset')" value="未设置" />
          <el-option :label="t('student.index.iq_passed')" value="同意面试" />
          <el-option :label="t('student.index.iq_indeterminate')" value="候补面试" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewSelected')" prop="interviewSelected">
        <el-select
          v-model="queryParams.interviewSelected"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewSelected')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('common.yes')" :value="true" />
          <el-option :label="t('common.no')" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.fields.interviewStatus')" prop="interviewStatus">
        <el-select
          v-model="queryParams.interviewStatus"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewStatus')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('student.index.isUninterview')" value="未面试" />
          <el-option :label="t('student.index.isUnsetInterview')" value="未设置面试状态" />
          <el-option :label="t('student.index.isPassed')" value="面试合格" />
          <el-option :label="t('student.index.isIndeterminate')" value="面试合格候补" />
          <el-option :label="t('student.index.isFail')" value="面试不合格" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('student.index.interviewScore')" prop="interviewScore">
        <el-input
          v-model="queryParams.interviewScore"
          :placeholder="t('common.inputTextPlaceholder') + t('student.index.score')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('student.fields.chioceStatus')" prop="chioceStatus">
        <el-select
          v-model="queryParams.chioceStatus"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.chioceStatus')"
          clearable
          class="!w-160px"
          :disabled="advancedSearching"
          @change="handleQuery"
        >
          <el-option :label="t('student.index.dcUnoperate')" value="未操作" />
          <el-option :label="t('student.index.dcNotChosen')" value="未双选" />
          <el-option :label="t('student.index.dcChosen')" value="已双选" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" :disabled="advancedSearching">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery" :disabled="advancedSearching">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleBatchSetInterview"
          v-hasPermi="['recruit:student:update']"
          :disabled="selectList.length === 0"
        >
          <Icon icon="ep:finished" class="mr-5px" />
          {{ t('student.index.batchSetInterview') }}
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['recruit:student:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          {{ t('action.export') }}
        </el-button>
        <SendChatRecruit :list="selectListObject" />
        <el-button
          type="success"
          plain
          @click="handleImportSummerCamp"
          :disabled="!queryParams.batchId"
        >
          <Icon icon="ep:upload" class="mr-5px" />
          {{ t('student.index.importSummerCamp') }}
        </el-button>
        <!--        <ConditionTree-->
        <!--          id="recruitstudent-condition-tree"-->
        <!--          :schemata="studentSchema"-->
        <!--          @search="handleAdvanceSearch"-->
        <!--        />-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iRecruitStudent-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :row-config="{ keyField: 'id' }"
      :checkbox-config="{ reserve: true }"
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
      @checkbox-all="collectSelect"
      @checkbox-change="collectSelect"
    >
      <vxe-column type="checkbox" width="50" align="center" fixed="left" />
      <vxe-column
        :title="t('student.fields.studentName')"
        field="studentName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        fixed="left"
      />
      <vxe-column
        :title="t('student.fields.recruitType')"
        field="recruitType"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.phone')"
        field="phone"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.batchId')"
        field="batchId"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          {{ batchOptions.find((item) => item.id === scope.row.batchId)?.batchName }}
        </template>
      </vxe-column>
      <vxe-column
        :title="t('student.fields.recruitStatus')"
        field="recruitStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <!--      <vxe-column-->
      <!--        :title="t('student.fields.recruitTime')"-->
      <!--        field="recruitTime"-->
      <!--        align="center"-->
      <!--        show-header-overflow-->
      <!--        show-overflow-->
      <!--        min-width="100"-->
      <!--        :formatter="vxeDateFormatter"-->
      <!--      />-->
      <vxe-column
        :title="t('student.fields.interviewQuality')"
        field="interviewQuality"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.interviewSelected')"
        field="interviewSelected"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          {{ scope.row.interviewSelected ? t('common.yes') : t('common.no') }}
        </template>
      </vxe-column>
      <vxe-column
        :title="t('student.fields.interviewDate')"
        field="interviewDate"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter2"
      />
      <vxe-column
        :title="t('student.fields.interviewTime')"
        field="interviewTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.interviewStatus')"
        field="interviewStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.interviewScore')"
        field="interviewScore"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.chioceStatus')"
        field="chioceStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.chioceMentorId')"
        field="chioceMentorName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.birthdate')"
        field="birthdate"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter2"
      />
      <vxe-column
        :title="t('student.fields.gender')"
        field="gender"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.politicalStatus')"
        field="politicalStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.ethnic')"
        field="ethnic"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.identityNumber')"
        field="identityNumber"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.contactAddress')"
        field="contactAddress"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.email')"
        field="email"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.university')"
        field="university"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.is985')"
        field="is985"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeBooleanFormatter"
      />
      <vxe-column
        :title="t('student.fields.is211')"
        field="is211"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeBooleanFormatter"
      />
      <vxe-column
        :title="t('student.fields.isFirstClass')"
        field="isFirstClass"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeBooleanFormatter"
      />
      <vxe-column
        :title="t('student.fields.studyMajor')"
        field="studyMajor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.applyMajor')"
        field="applyMajor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.majorRank')"
        field="majorRank"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.majorTotal')"
        field="majorTotal"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.classRank')"
        field="classRank"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.classTotal')"
        field="classTotal"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.cet4Score')"
        field="cet4Score"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.cet6Score')"
        field="cet6Score"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('student.fields.attachment')"
        field="attachment"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <FileObjectDownload :file-json="scope.row.attachment" />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('student.fields.remark')"
        field="remark"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :visible="false"
      />
      <vxe-column
        :title="t('student.fields.createTime')"
        field="createTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
        :visible="false"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="210"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleSetInterview(scope.row)"
            v-hasPermi="['recruit:student:update']"
            :disabled="scope.row.interviewStatus !== '未面试'"
          >
            {{ t('student.index.setInterview') }}
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleCancelInterview(scope.row)"
            v-hasPermi="['recruit:student:update']"
            :disabled="
              !(scope.row.interviewQuality === '同意面试' && scope.row.interviewStatus === '未面试')
            "
          >
            {{ t('student.index.cancelInterview') }}
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleChangeInterview(scope.row)"
            v-hasPermi="['recruit:student:update']"
            :disabled="
              !(scope.row.interviewQuality === '同意面试' && scope.row.interviewStatus === '未面试')
            "
          >
            {{ t('student.index.changeInterview') }}
          </el-button>
          <el-button link type="primary" @click="handleViewDoubleChoose(scope.row)">
            {{ t('student.index.viewDoubleChoose') }}
          </el-button>
          <!--          <el-button-->
          <!--            link-->
          <!--            type="primary"-->
          <!--            @click="openForm('update', scope.row.id)"-->
          <!--            v-hasPermi="['recruit:student:update']"-->
          <!--          >-->
          <!--            {{ t('action.edit') }}-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            link-->
          <!--            type="danger"-->
          <!--            @click="handleDelete(scope.row.id)"-->
          <!--            v-hasPermi="['recruit:student:delete']"-->
          <!--          >-->
          <!--            {{ t('action.delete') }}-->
          <!--          </el-button>-->
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StudentForm ref="formRef" @success="getList" />
  <!-- 导入夏令营信息 -->
  <SummerCampExcelImport
    v-if="queryParams.batchId"
    ref="importFormRef"
    :batch-id="queryParams.batchId"
    @success="getList"
  />

  <!-- 批量设置面试资格弹窗 -->
  <Dialog
    :title="
      batchInterviewMode ? t('student.index.batchSetInterview') : t('student.index.setInterview')
    "
    v-model="dialogVisible"
    width="400"
    :fullscreen="false"
  >
    <el-alert
      v-if="batchInterviewMode"
      class="!mb-[10px]"
      :title="t('student.index.batchSelectTips', { num: selectList.length })"
      type="success"
      :closable="false"
    />
    <el-form>
      <el-form-item :label="t('student.fields.interviewQuality')">
        <el-select
          v-model="interviewQuality"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewQuality')"
        >
          <el-option :label="t('student.index.iq_passed')" value="同意面试" />
          <el-option :label="t('student.index.iq_indeterminate')" value="候补面试" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>

  <!-- 修改面试时间 -->
  <ChangeStudentInterviewSlot ref="changeSlotRef" @success="getList" />
  <DoubleChoiceDetail ref="choiceDetailRef" @revoked="getList" />
</template>

<script setup lang="ts">
import { vxeDateFormatter, vxeDateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { StudentApi, StudentVO } from '@/api/recruit/recruitstudent'
import StudentForm from './StudentForm.vue'
import SummerCampExcelImport from './components/SummerCampExcelImport.vue'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { BatchApi, BatchVO } from '@/api/recruit/batch'
import SendChatRecruit from '@/components/SendChatRecruit/index.vue'
import ChangeStudentInterviewSlot from '@/views/recruit/recruitstudent/components/ChangeStudentInterviewSlot.vue'
import DoubleChoiceDetail from '@/views/recruit/doublechioce/DoubleChoiceDetail.vue'
import FileObjectDownload from '@/components/FileObjectDownload/index.vue'
import { vxeBooleanFormatter } from '@/utils/formatter'

/** 报名学生 列表 */
defineOptions({ name: 'RecruitStudent' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<StudentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  advanced: undefined,
  accountId: undefined,
  studentName: undefined,
  recruitType: undefined,
  phone: undefined,
  batchId: undefined,
  recruitStatus: undefined,
  recruitTime: [],
  interviewQuality: undefined,
  interviewSelected: undefined,
  interviewDate: [],
  interviewTime: [],
  interviewStatus: undefined,
  interviewId: undefined,
  interviewGroupId: undefined,
  interviewSlotId: undefined,
  interviewSlotTime: undefined,
  interviewScore: undefined,
  chioceStatus: undefined,
  chioceMentorId: undefined,
  birthdate: [],
  gender: undefined,
  politicalStatus: undefined,
  ethnic: undefined,
  identityNumber: undefined,
  contactAddress: undefined,
  email: undefined,
  university: undefined,
  is985: undefined,
  is211: undefined,
  isFirstClass: undefined,
  studyMajor: undefined,
  applyMajor: undefined,
  majorRank: undefined,
  majorTotal: undefined,
  classRank: undefined,
  classTotal: undefined,
  cet4Score: undefined,
  cet6Score: undefined,
  attachment: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

const selectList = ref<string[]>([])
const selectListObject = ref<StudentVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StudentApi.getStudentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof StudentForm>>()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 导入 */
const importFormRef = ref<InstanceType<typeof SummerCampExcelImport>>()
const handleImportSummerCamp = async () => {
  const name = batchOptions.value.find((item) => item.id === queryParams.batchId)?.batchName
  try {
    await message.confirm(t('student.index.importSummerCampTip', { name }))
  } catch {
    return
  }
  importFormRef.value?.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StudentApi.deleteStudent(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StudentApi.exportStudent(queryParams)
    download.excel(data, '报名学生.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleAdvanceSearch = (base64Advanced?: string) => {
  ;(queryParams as any).advanced = base64Advanced
  advancedSearching.value = !!base64Advanced
  handleQuery()
}

const collectSelect = () => {
  selectList.value = [
    ...(tableRef.value?.getCheckboxRecords() ?? []),
    ...(tableRef.value?.getCheckboxReserveRecords() ?? [])
  ].map((item) => item.id)
  selectListObject.value = [
    ...(tableRef.value?.getCheckboxRecords() ?? []),
    ...(tableRef.value?.getCheckboxReserveRecords() ?? [])
  ]
}

const dialogVisible = ref(false)
const formLoading = ref(false)
const interviewQuality = ref('')
const currentRow = ref<StudentVO>()
const batchInterviewMode = ref(false)

const handleSetInterview = (row: StudentVO) => {
  batchInterviewMode.value = false
  currentRow.value = row
  interviewQuality.value = row.interviewQuality ?? ''
  dialogVisible.value = true
}

const handleBatchSetInterview = () => {
  batchInterviewMode.value = true
  interviewQuality.value = ''
  dialogVisible.value = true
}

// 解除面试时间
const handleCancelInterview = async (row: StudentVO) => {
  try {
    await message.confirm(t('student.index.cancelInterviewTips'))
  } catch {
    return
  }
  await StudentApi.cancelInterviewTime(row.id)
  message.success(t('common.operSuccess'))
  await getList()
}

const changeSlotRef = ref<InstanceType<typeof ChangeStudentInterviewSlot>>()
// 修改面试时间
const handleChangeInterview = async (row: StudentVO) => {
  changeSlotRef.value?.open(row.id)
}

// 双选详情弹窗
const choiceDetailRef = ref<InstanceType<typeof DoubleChoiceDetail>>()
// 查看双选结果
const handleViewDoubleChoose = async (row: StudentVO) => {
  choiceDetailRef.value?.open(row.id)
}

const submitForm = async () => {
  if (interviewQuality.value === '') {
    message.error(t('common.selectTextPlaceholder') + t('student.fields.interviewQuality'))
    return
  }
  formLoading.value = true
  try {
    let ids
    if (batchInterviewMode.value) {
      ids = selectList.value
    } else {
      ids = [currentRow.value?.id]
    }

    await StudentApi.batchSetInterview({
      interviewQuality: interviewQuality.value,
      recruitStudentIds: ids
    })
    message.success(t('common.operSuccess'))
    dialogVisible.value = false
    getList()
  } catch {
  } finally {
    formLoading.value = false
  }
}

const batchOptions = ref<BatchVO[]>([])
const getBatchSelectOptions = async () => {
  batchOptions.value = await BatchApi.getSelectList()
}

/** 初始化 **/
onMounted(async () => {
  // 先查询所有计划
  await getBatchSelectOptions()
  // 然后查询最新计划的数据
  queryParams.batchId = batchOptions.value[0]?.id ?? undefined
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>

<style lang="scss" scoped>
:deep(.vxe-cell .el-button + .el-button) {
  margin-left: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 15px !important;
}
</style>
